package com.yellowpage.bigdata.service;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class AutoMailUserServiceImpl {
	public static void main(String[] args) throws AddressException {
		List<String> userList = AutoMailUserServiceImpl.getUserList();
		System.out.println(userList.toString());
		System.out.println(userList.size());
		InternetAddress[] to=new InternetAddress[userList.size()];// tto为发邮件的目的地（收件人信箱）
		for (int i = 0; i < to.length; i++) {
			to[i]=new InternetAddress(userList.get(i));
		}
		System.out.println(to[0]);
		System.out.println(to[1]);
	}
	public static List<String> getUserList(){
		InputStream in = AutoMailUserServiceImpl.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties props=new Properties();
        try {
			props.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        String driver = props.getProperty("jdbc.driverClassName");
        String url = props.getProperty("jdbc.url");
        String username = props.getProperty("jdbc.username");
        String password = props.getProperty("jdbc.password");
        Connection conn = null;
        PreparedStatement statement = null;
        List<String> userlists=new ArrayList<String>();
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
            String sql="SELECT u.EMAIL FROM AUTO_MAIL_USER u ";
			statement = conn.prepareStatement(sql);
			ResultSet rs = statement.executeQuery();
			
			while(rs.next()){
				String email = rs.getString("u.EMAIL");
				userlists.add(email);
			}
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
			try {
				if (statement != null) {
					statement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
        
		return userlists;
		
	}
	
}
